package com.liuqi.loggers.core.util;

import com.liuqi.loggers.core.Constants;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日志文件滚动, 辅助工具类, 检查日期是否需要滚动
 *
 * @author liuqi
 * @date 2024/11/5 16:38
 **/
public class RollingUtil {

    /**
     * @return  当前时间的日历对象
     */
    public static Calendar calendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar;
    }

    /**
     * 下个月份的时间戳
     *
     * @param interval  next
     * @return
     */
    public static long nextMonth(int interval) {
        try {
            Calendar calendar = calendar();
            calendar.add(Calendar.MONTH, interval);
            return new SimpleDateFormat("yyyyMM")
                    .parse( calendar.get(Calendar.YEAR) + "" + (calendar.get(Calendar.MONTH) + 1))
                    .getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 上个月份
     *
     * @param interval  pre
     * @return
     */
    public static String preMonth(int interval) {
        Calendar calendar = calendar();
        calendar.add(Calendar.MONTH, -interval);
        return new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
    }

    /**
     * 下个日期的时间戳
     *
     * @param interval next
     * @return
     */
    public static long nextDay(int interval) {
        try {
            Calendar calendar = calendar();
            calendar.add(Calendar.DAY_OF_MONTH, interval);
            return new SimpleDateFormat("yyyy-MM-dd")
                    .parse(calendar.get(Calendar.YEAR)
                            + "-" + (calendar.get(Calendar.MONTH) + 1)
                            + "-" + calendar.get(Calendar.DAY_OF_MONTH))
                    .getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 上个日期
     *
     * @param interval  pre
     * @return
     */
    public static String preDay(int interval) {
        Calendar calendar = calendar();
        calendar.add(Calendar.DAY_OF_MONTH, -interval);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

    /**
     * 下个时间点的时间戳
     *
     * @param interval
     * @return
     */
    public static long nextHour(int interval) {
        try {
            Calendar calendar = calendar();
            calendar.add(Calendar.HOUR_OF_DAY, interval);
            return new SimpleDateFormat("yyyy-MM-dd HH")
                    .parse(calendar.get(Calendar.YEAR)
                            + "-" + (calendar.get(Calendar.MONTH) + 1)
                            + "-" + calendar.get(Calendar.DAY_OF_MONTH)
                            + " " + calendar.get(Calendar.HOUR_OF_DAY))
                    .getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 上个时间点
     *
     * @param interval
     * @return
     */
    public static String preHour(int interval) {
        Calendar calendar = calendar();
        calendar.add(Calendar.HOUR_OF_DAY, -interval);
        return new SimpleDateFormat("yyyy-MM-dd-HH").format(calendar.getTime());
    }

    /**
     * 获取文件名后缀
     *
     * @param filename 文件名称
     * @return
     */
    public static String suffix(String filename) {
        int index;
        if ((index = filename.lastIndexOf(Constants.DOT)) != -1) {
            return filename.substring(index + 1);
        }
        return "";
    }

    /**
     * 获取文件名称
     *
     * @param file
     * @return
     */
    public static String filename(File file) {
        String name = file.getName();
        int index;
        if ((index = name.lastIndexOf(Constants.DOT)) != -1) {
            return name.substring(0, index);
        }
        return name;
    }

}
